package com.start.common.enums;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class StringToEnumConvertFactory extends JsonDeserializer<StartBaseStringEnum> implements ConverterFactory<String, StartBaseStringEnum> {
    private static final Map<Class, Converter> CONVERTERS = new HashMap<>();

    @Override
    public <T extends StartBaseStringEnum> Converter<String, T> getConverter(Class<T> clazz) {
        Converter<String, T> converter = CONVERTERS.get(clazz);
        if (converter == null) {
            converter = new StringToEnumConvert(clazz);
            CONVERTERS.put(clazz, converter);
        }
        return converter;
    }

    @Override
    public StartBaseStringEnum deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return null;
    }
}
